﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Microsoft.Xna.Framework.Content;
using DARE.Properties;

namespace DARE
{
    public class CMultiTexturingBasicEffect: CBasicEffect
    {
        #region fields

       

        #endregion
                
        #region properties

        
        
        #endregion

        #region ctor

        /// <summary>
        /// Constructs the CBasicEffect class with a default effect file ("Shaders/IntensityEffect").
        /// If you want to use this ctor, you have to create this effect file at this path.
        /// You also have to implement a technique named "SpecularLight" which will be the default technique.
        /// If you don't want to, use the second ctor.
        /// </summary>
        /// <param name="content">The content manager you want to use for this effect</param>
        public CMultiTexturingBasicEffect()
        {
            Name = "Shaders/MultiTexturingBasicEffect";
#if WINDOWS_PHONE
            m_effect = new BasicEffect(DARE.CDare.Instance.GraphicsDevice);
#else
            m_effect = CResourceMgr.Load<Effect>(DARE.Properties.Resources.MultiTexturingBasicEffect, Name);
            m_techniquePrefix = "MultiTexturing";
            Initialize(m_techniquePrefix + "VertexLightBasic");
#endif
            

        }
        
        #endregion

        #region Method


        public override CBasicEffect ProcessEffect(CRender3D render, CEffect effect)
        {
            base.ProcessEffect(render, effect);
#if !WINDOWS_PHONE
            int nbTexturesSet = 0;
            if (effect.ColorMap != null)
                nbTexturesSet++;
            m_effect.Parameters["Offset1"].SetValue(effect.ColorMapOffset1);
            m_effect.Parameters["Tiles1"].SetValue(effect.ColorMapTiles1);
            if (effect.ColorMap1 != null)
            {
                m_effect.Parameters["ColorMap1"].SetValue(effect.ColorMap1);
                nbTexturesSet++;
            }
            m_effect.Parameters["Offset2"].SetValue(effect.ColorMapOffset2);
            m_effect.Parameters["Tiles2"].SetValue(effect.ColorMapTiles2);
            if (effect.ColorMap2 != null)
            {
                m_effect.Parameters["ColorMap2"].SetValue(effect.ColorMap2);
                nbTexturesSet++;
            }
            m_effect.Parameters["Offset3"].SetValue(effect.ColorMapOffset3);
            m_effect.Parameters["Tiles3"].SetValue(effect.ColorMapTiles3);
            if (effect.ColorMap3 != null)
            {
                m_effect.Parameters["ColorMap3"].SetValue(effect.ColorMap3);
                nbTexturesSet++;
            }
            m_effect.Parameters["OffsetMultiTexturingMap"].SetValue(effect.MultiTexturingMapOffset);
            m_effect.Parameters["TilesMultiTexturingMap"].SetValue(effect.MultiTexturingMapTiles);
            if (effect.MultiTexturingMap != null)
                m_effect.Parameters["MultiTexturingMap"].SetValue(effect.MultiTexturingMap);
            else
            {
                Texture2D td = new Texture2D(DARE.CDare.Instance.GraphicsDevice, 1, 1);
                td.SetData(new Color[1] { new Color((effect.ColorMap != null ? 255 / nbTexturesSet : 0), (effect.ColorMap1 != null ? 255 / nbTexturesSet : 0), (effect.ColorMap2 != null ? 255 / nbTexturesSet : 0), (effect.ColorMap3 != null ? 255 - (255 / nbTexturesSet) : 0)) });
                m_effect.Parameters["MultiTexturingMap"].SetValue(td);
            }
            m_effect.Parameters["NbTexturesSet"].SetValue(nbTexturesSet);
#endif
            return this;
        }


        #endregion
    }
}
